package com.ruoyi.system.controller;

import com.ruoyi.system.domain.Question;
import com.ruoyi.system.domain.vo.Param;
import com.ruoyi.system.service.IQuestionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/question")
public class QuestionController {

    private static final Logger logger = LoggerFactory.getLogger(QuestionController.class);

    @Autowired
    private IQuestionService questionService;

    // 查询试题列表
    @GetMapping("list")
    public List<Question> listQuestion(Integer id) {
        try {
            logger.info("开始查询试题列表，id: {}", id);
            List<Question> questions = questionService.lishQuestion(id);
            logger.info("查询试题列表成功，返回结果数量: {}", questions.size());
            return questions;
        } catch (Exception e) {
            logger.error("查询试题列表失败，id: {}", id, e);
            throw new RuntimeException("查询试题列表失败", e);
        }
    }

    // 查询题库信息
    @GetMapping("listBank")
    public String listBank(Integer id) {
        try {
            logger.info("开始查询题库信息，id: {}", id);
            String result = questionService.listBank(id);
            logger.info("查询题库信息成功，返回结果: {}", result);
            return result;
        } catch (Exception e) {
            logger.error("查询题库信息失败，id: {}", id, e);
            throw new RuntimeException("查询题库信息失败", e);
        }
    }

    // 根据条件查询试题
    @PostMapping("queryQuestionsByCondition")
    public List<Question> queryQuestionsByCondition(@RequestBody Param param) {
        try {
            logger.info("开始根据条件查询试题，参数: {}", param);
            List<Question> questions = questionService.queryQuestionsByCondition(param);
            logger.info("根据条件查询试题成功，返回结果数量: {}", questions.size());
            return questions;
        } catch (Exception e) {
            logger.error("根据条件查询试题失败，参数: {}", param, e);
            throw new RuntimeException("根据条件查询试题失败", e);
        }
    }

    // 添加试题
    @PostMapping("addQuestion")
    public boolean addQuestion(@RequestBody Param param) {
        try {
            logger.info("开始添加试题，参数: {}", param);
            questionService.addQuestion(param);
            logger.info("添加试题成功");
            return true;
        } catch (Exception e) {
            logger.error("添加试题失败，参数: {}", param, e);
            return false;
        }
    }

    // 修改试题
    @RequestMapping("updateQuestion")
    public boolean updateQuestion(@RequestBody Param param) {
        try {
            logger.info("开始修改试题，参数: {}", param);
            questionService.updateQuestion(param);
            logger.info("修改试题成功");
            return true;
        } catch (Exception e) {
            logger.error("修改试题失败，参数: {}", param, e);
            return false;
        }
    }

    // 删除试题
    @DeleteMapping("deleteQuestion")
    public boolean deleteQuestion(@RequestParam("id") Integer id) {
        try {
            logger.info("开始删除试题，参数: {}", id);
            questionService.deleteQuestion(id);
            logger.info("删除试题成功");
            return true;
        } catch (Exception e) {
            logger.error("删除试题失败，参数: {}", id, e);
            return false;
        }
    }
}